// pages/component/thres/thres.js
const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    region: {
      type: Object,
      value: undefined
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // thresList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
  
    groupChange: function (event) {

      // let currentPageIndex =
      //   this.setData({
      //     //拿到当前索引并动态改变
      //     currentIndex: e.currentTarget.dataset.idx
      //   });
      // console.error('groupChange', event.detail.value);
      var _this = this;
      var items = _this.data.thresList;
      var currentItem;
      for (var i = 0; i < items.length; ++i) {
        if (i == event.detail.value) {
          currentItem = items[i];
          items[i].checked = true;
          continue;
        }
        items[i].checked = false;
      };
      // _this.setData({
      //   thresList: items, description: currentItem.description, tMax: currentItem.tMax, tMin: currentItem.tMin, tDiff: currentItem.tDiff      });
      // console.error(event.currentTarget.dataset.idx);
      _this.setData({ thresList: items, currentIndex: event.detail.value});
     
    },
    pagechange: function (event) {
      // console.log(JSON.stringify(event));
      //如果是滑动手势切换,则调用该方法;
      if ("touch" === event.detail.source) {
        var _this = this;
        var items = _this.data.thresList;
        var currentItem;
        for (var i = 0; i < items.length; ++i) {
          if (i == event.detail.current) {
            currentItem = items[i];
            items[i].checked = true;
            continue;
          }
          items[i].checked = false;
        };
        _this.setData({
          thresList: items,
          currentIndex: event.detail.current
        });
        // console.log(JSON.stringify(items));
      }

    }
  },
  lifetimes: {
    ready: function () {
      var _this = this;
      // console.log("thres region = " + JSON.stringify(_this.data.region));
      api.listThresByRegion(_this.data.region.gSerial, _this.data.region.deviceRegion,function(res){
        var result = res.data;
        // console.log(JSON.stringify(result));
        if(result.status!=0){
          //失败
          return;
        }
        var items = result.datas;
        for (var i = 0; i < items.length; ++i) {
          if(i==0){
            items[i].checked = true;
            continue;
          }
          items[i].checked = false;
        };
        
        
       
        // _this.setData({ thresList: result.datas, description: result.datas[0].description,  tMax: result.datas[0].tMax, tMin: result.datas[0].tMin, tDiff: result.datas[0].tDiff});
        _this.setData({ thresList: result.datas});
      });
    }
  }
})
